package net.ssta.util.fusionchart; 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.jdom.Document;
import org.jdom.Element;  
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * @deprecated
 * @author HillEric
 *
 */
class XmlBuilder { 
	private static Element getStylesElement(FusionCharts cb) {
		List fontList = cb.getFontList();
		if (fontList.size() == 0) {
			return null;
		} else {
			Element styles = new Element("styles");
			Element definition = new Element("definition");
			Element application = new Element("application");
			Element style = null;
			Element apply = null;
			String styleName = null;
			FontArea fa = null;
			ChartsFont cf = null;
			for (int i = 0; i < fontList.size(); i++) {
				fa = (FontArea) fontList.get(i);
				cf = fa.getFont();
				styleName = fa.getToObject() + "Font";
				style = new Element("style");
				style.setAttribute("type", "font");
				style.setAttribute("name", styleName);
				if (cf.getFontName() != null) {
					style.setAttribute("font", cf.getFontName());
				}
				if (cf.getFontColor() != null) {
					style.setAttribute("Color", cf.getFontColor());
				}
				if (cf.getFontSize() != null) {
					style.setAttribute("size", cf.getFontSize());
				}
				if (cf.getBgColor() != null) {
					style.setAttribute("bgColor", cf.getBgColor());
				}
				if (cf.getBorderColor() != null) {
					style.setAttribute("borderColor", cf.getBorderColor());
				}
				if (cf.isBold()) {
					style.setAttribute("Bold", "1");
				}
				if (cf.isItalic()) {
					style.setAttribute("Italic", "1");
				}
				if (cf.isUnderline()) {
					style.setAttribute("Underline", "1");
				}
				if (cf.isHTML()) {
					style.setAttribute("isHTML", "1");
				}
				definition.addContent(style);
				apply = new Element("apply");
				apply.setAttribute("toObject", fa.getToObject());
				apply.setAttribute("styles", styleName);
				application.addContent(apply);
			}
			styles.addContent(definition);
			styles.addContent(application);
			return styles;
		}
	}

	private static Element getChartElement(FusionCharts cb) {
		Element chart = new Element("chart");
		chart.setAttribute("caption", cb.getTitle());
		chart.setAttribute("numberPrefix", cb.getNumberPrefix());
		chart.setAttribute("numberSuffix", cb.getNumberSuffix());
		chart.setAttribute("showSum", cb.getShowSum());
		if(cb.getBaseFontSize()!= null){
		    chart.setAttribute("baseFontSize", cb.getBaseFontSize());
		}
		if(cb.getOutCnvBaseFontSize()!= null){
		    chart.setAttribute("outCnvBaseFontSize", cb.getOutCnvBaseFontSize());
		}else{
			chart.setAttribute("outCnvBaseFontSize", "12");
		}
		if (cb.getSubTitle() != null) {
			chart.setAttribute("subcaption", cb.getSubTitle());
		}
		if(cb.getShowLegend()!=null)
		{
			chart.setAttribute("showLegend", cb.getShowLegend());
		}
		if (cb.isShowName()) {
			chart.setAttribute("shownames", "1");
		} else {
			chart.setAttribute("shownames", "0");
		}
		if (cb.isShowValue()) {
			chart.setAttribute("showvalues", "1");
		} else {
			chart.setAttribute("showvalues", "0");
		}
		if (cb.getXAxisName() != null) {
			chart.setAttribute("xAxisName", cb.getXAxisName());
			chart.setAttribute("rotateXAxisName", "0");
		}
		if (cb.getPYAxisName() != null) {
			chart.setAttribute("PYAxisName", cb.getPYAxisName());
		}
		if (cb.getSYAxisName() != null) {
			chart.setAttribute("SYAxisName", cb.getSYAxisName());
		}
		if (cb.getYAxisName() != null) {
			chart.setAttribute("yAxisName", cb.getYAxisName());
			chart.setAttribute("rotateYAxisName", "0");
		}
		if (cb.getPlotFillRatio() != null) {
			chart.setAttribute("plotFillRatio", cb.getPlotFillRatio());
		}
		if (cb.getYAxisMinValue() != null) {
			chart.setAttribute("yAxisMinValue", cb.getYAxisMinValue());
		}
		if (cb.getYAxisMaxValue() != null) {
			chart.setAttribute("yAxisMaxValue", cb.getYAxisMaxValue());
		}
		Map map = cb.getChartAttribute();
		Iterator<Entry<String, String>> it = map.entrySet().iterator();
	    while (it.hasNext()) 
	    {
	    	Map.Entry entry = it.next();
	    	String key = (String) entry.getKey();
	    	String value =  (String) entry.getValue();
	    	chart.setAttribute(key,value);
	    }
		
		return chart;
	}

	/**
	 * 获取不分组XML
	 * 
	 * @param cb
	 * @param categoryName
	 * @param chartDiv
	 * @param chartId
	 * @return
	 */
	public static Document getSimpleXML(FusionCharts cb, String categoryName,
			String chartDiv, String chartId) {
		Element chart = getChartElement(cb);
		Document doc = new Document(chart);
		List data = cb.getDataByCategory(categoryName);
		if (data == null) {
			System.out.println("没有分类:" + categoryName);
			data.size();
		}
		Element set = null;
		ChartsData cd = null;
		for (int i = 0; i < data.size(); i++) {
			cd = (ChartsData) data.get(i);
			set = new Element("set");
			set.setAttribute("label", cd.getName());
			if(cd.getToolText()!=null)
			{
				set.setAttribute("toolText",cd.getToolText());
			}
			set.setAttribute("value", String.valueOf(cd.getValue()));
			if (cd.getShowValue() != null) {
				set.setAttribute("showValue", cd.getShowValue());
			}
			String color = cb.getSimpleColor(cd.getName());
			if (color != null) {
				set.setAttribute("color", color);
			}
			if (cd.getLinkType() == cb.linkType_custom) {
				set.setAttribute("link", cd.getUrl());
			} else if (cd.getLinkType() == cb.linkType_ajax) {
				StringBuffer url = new StringBuffer();
				/*url.append("javascript:_updateCharts('").append(chartDiv)
						.append("','").append(chartId).append("','").append(
								cd.getSwf()).append("','")
						.append(cb.getWidth()).append("','").append(
								cb.getHeight()).append("','").append(
								cd.getClassname()).append("','").append(
								cd.getParams()).append("','").append(
								BaseConfig.getContextPath()).append("');");*/
				set.setAttribute("link", url.toString());
			}
			chart.addContent(set);
			Element styles = getStylesElement(cb);
			if (styles != null) {
				chart.addContent(styles);
			}
		}
		return doc;
	}

	/**
	 * 获取分组层叠XML
	 * 
	 * @param cb
	 * @param chartDiv
	 * @param chartId
	 * @return
	 */
	public static Document getMssByNamesXML(FusionCharts cb, String chartDiv,
			String chartId) {
		Element chart = getChartElement(cb);
		Document doc = new Document(chart);
		Element categories = new Element("categories");
		Element category = null;
		List categorieList = cb.getCategorys();
		String nameStr = null;
		String categoryStr = null;
		for (int i = 0; i < categorieList.size(); i++) {
			categoryStr = (String) categorieList.get(i);
			category = new Element("category");
			category.setAttribute("label", categoryStr);
			categories.addContent(category);
		}
		chart.addContent(categories);
		Element dataset = null;
		Element o_dataset = null;
		Element set = null;
		ChartsData cd = null;
		List mssByNames = cb.getMssByNames();
		String[] names = null;
		for (int k = 0; k < mssByNames.size(); k++) {
			o_dataset = new Element("dataset");
			names = (String[]) mssByNames.get(k);
			for (int i = 0; i < names.length; i++) {
				nameStr = names[i];
				dataset = new Element("dataset");
				dataset.setAttribute("seriesName", nameStr);
				String rendType = cb.getNameRendAs(nameStr);
				if (rendType != null) {
					dataset.setAttribute("renderAs", rendType);
				}
				String color = cb.getCategoryColor(nameStr);
				if (color != null) {
					dataset.setAttribute("color", color);
				}
				if (cb.isShowValue()) {
					dataset.setAttribute("showvalues", "1");
				} else {
					dataset.setAttribute("showvalues", "0");
				}
				for (int j = 0; j < categorieList.size(); j++) {
					categoryStr = (String) categorieList.get(j);
					cd = cb.getDataByCategoryAndName(categoryStr, nameStr);
					if (cb == null) {
						System.out.println("数据未设置，数据分类为" + categoryStr
								+ "数据名称为" + nameStr);
						cd.getName();
					}
					set = new Element("set");
					set.setAttribute("value", String.valueOf(cd.getValue()));
					if (cd.getShowValue() != null) {
						set.setAttribute("showValue", cd.getShowValue());
					}
					if(cd.getToolText()!=null)
					{
						set.setAttribute("toolText",cd.getToolText());
					}
					if (cd.getLinkType() == cb.linkType_custom) {
						set.setAttribute("link", cd.getUrl());
					} else if (cd.getLinkType() == cb.linkType_ajax) {
						StringBuffer url = new StringBuffer();
						/*url.append("javascript:_updateCharts('").append(
								chartDiv).append("','").append(chartId).append(
								"','").append(cd.getSwf()).append("','")
								.append(cb.getWidth()).append("','").append(
										cb.getHeight()).append("','").append(
										cd.getClassname()).append("','")
								.append(cd.getParams()).append("','").append(
										BaseConfig.getContextPath()).append(
										"');");*/
						set.setAttribute("link", url.toString());
					}
					dataset.addContent(set);
				}
				o_dataset.addContent(dataset);
			}
			chart.addContent(o_dataset);
		}
		Element styles = getStylesElement(cb);
		if (styles != null) {
			chart.addContent(styles);
		}
		return doc;
	}

	/**
	 * 获取分组XML
	 * 
	 * @param cb
	 * @param chartDiv
	 * @param chartId
	 * @return
	 */
	public static Document getCategorysXML(FusionCharts cb) {
		Element chart = getChartElement(cb);
		Document doc = new Document(chart);
		Element categories = new Element("categories");
		Element category = null;
		List nameList = cb.getNames();
		List categorieList = cb.getCategorys();
		String nameStr = null;
		String categoryStr = null;
		for (int i = 0; i < categorieList.size(); i++) {
			categoryStr = (String) categorieList.get(i);
			category = new Element("category");
			category.setAttribute("label", categoryStr);
			categories.addContent(category);
		}
		chart.addContent(categories);
		Element dataset = null;
		Element set = null;
		ChartsData cd = null;
		for (int i = 0; i < nameList.size(); i++) {
			nameStr = (String) nameList.get(i);
			dataset = new Element("dataset");
			dataset.setAttribute("seriesName", nameStr);
			String rendType = cb.getNameRendAs(nameStr);
			if (rendType != null) {
				dataset.setAttribute("renderAs", rendType);
			}
			String color = cb.getCategoryColor(nameStr);
			if (color != null) {
				dataset.setAttribute("color", color);
			}
			if (cb.isShowValue()) {
				dataset.setAttribute("showvalues", "1");
			} else {
				dataset.setAttribute("showvalues", "0");
			}
			for (int j = 0; j < categorieList.size(); j++) {
				categoryStr = (String) categorieList.get(j);
				cd = cb.getDataByCategoryAndName(categoryStr, nameStr);
				if (cb == null) {
					System.out.println("数据未设置，数据分类为" + categoryStr + "数据名称为"
							+ nameStr);
					cd.getName();
				}
				set = new Element("set");
				set.setAttribute("value", String.valueOf(cd.getValue()));
				if (cd.getShowValue() != null) {
					set.setAttribute("showValue", cd.getShowValue());
				}
				if(cd.getToolText()!=null)
				{
					set.setAttribute("toolText",cd.getToolText());
				}
				if (cd.getLinkType() == cb.linkType_custom) {
					set.setAttribute("link", cd.getUrl());
				} else if (cd.getLinkType() == cb.linkType_ajax) {
//					StringBuffer url = new StringBuffer();
//					url.append("javascript:_updateCharts('").append(chartDiv)
//							.append("','").append(chartId).append("','")
//							.append(cd.getSwf()).append("','").append(
//									cb.getWidth()).append("','").append(
//									cb.getHeight()).append("','").append(
//									cd.getClassname()).append("','").append(
//									cd.getParams()).append("','").append(
//									BaseConfig.getContextPath()).append("');");
//					set.setAttribute("link", url.toString());
				}
				dataset.addContent(set);

			}
			chart.addContent(dataset);
		}
		Element styles = getStylesElement(cb);
		if (styles != null) {
			chart.addContent(styles);
		}
		return doc;
	}
	
	/**
	 * 获取分组XML
	 * 
	 * @param cb
	 * @return
	 */
	public static Document getSetsXML(FusionCharts cb) {
		Element chart = getChartElement(cb);
		Document doc = new Document(chart);
		List nameList = cb.getNames();
		List categorieList = cb.getCategorys();
		String nameStr = null;
		String categoryStr = null;
		Element set = null;
		ChartsData cd = null;
		for (int i = 0; i < nameList.size(); i++) {
			nameStr = (String) nameList.get(i);
			set = new Element("set");
			set.setAttribute("label", nameStr);
			categoryStr = (String) categorieList.get(0);
			cd = cb.getDataByCategoryAndName(categoryStr, nameStr);
			set.setAttribute("value", String.valueOf(cd.getValue()));
			if (cd.getShowValue() != null) {
				set.setAttribute("showValue", cd.getShowValue());
			}
			if(cd.getToolText()!=null)
			{
				set.setAttribute("toolText",cd.getToolText());
			}
			if (cd.getLinkType() == cb.linkType_custom) {
				set.setAttribute("link", cd.getUrl());
			} else if (cd.getLinkType() == cb.linkType_ajax) {
//				StringBuffer url = new StringBuffer();
//				url.append("javascript:_updateCharts('").append(chartDiv)
//						.append("','").append(chartId).append("','")
//						.append(cd.getSwf()).append("','").append(
//								cb.getWidth()).append("','").append(
//								cb.getHeight()).append("','").append(
//								cd.getClassname()).append("','").append(
//								cd.getParams()).append("','").append(
//								BaseConfig.getContextPath()).append("');");
//				set.setAttribute("link", url.toString());
			}
			chart.addContent(set);
		}
		Element styles = getStylesElement(cb);
		if (styles != null) {
			chart.addContent(styles);
		}
		return doc;
	}

//	/**
//	 * 获得XML字符串
//	 * 
//	 * @param encoding
//	 *            字符集 UTF-8或GB2312或其他字符集
//	 * @return
//	 */
//	public String getXmlString(String encoding, Document doc) {
//		XMLOutputter xmlOut = new XMLOutputter();
//		xmlOut.setEncoding(encoding);
//		xmlOut.setNewlines(true);
//		return xmlOut.outputString(doc);
//	}

	public static void writeXmlFile(File file, String encoding, Document doc)
	{
		Format format = Format.getCompactFormat();
		format.setEncoding(encoding);
		
		format.setIndent("  ");

		//输出XML
		FileOutputStream fos = null;
		try
		{
			if (file.exists()) 	file.delete();

			XMLOutputter outputter = new XMLOutputter(format);
			fos = new FileOutputStream(file);
			outputter.output(doc, fos);
		} catch (Exception e) 
		{
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
			}
		}
	}

	public static Document getCombineXML(FusionCharts cb) 
	{
		Element chart = getChartElement(cb);
		Document doc = new Document(chart);
		Element categories = new Element("categories");
		Element category = null;
		List nameList = cb.getNames();
		List categorieList = cb.getCategorys();
		String nameStr = null;
		String categoryStr = null;
		for (int i = 0; i < categorieList.size(); i++) {
			categoryStr = (String) categorieList.get(i);
			category = new Element("category");
			category.setAttribute("label", categoryStr);
			categories.addContent(category);
		}
		chart.addContent(categories);
		Element dataset = null;
		Element set = null;
		ChartsData cd = null;
		for (int i = 0; i < nameList.size(); i++) {
			nameStr = (String) nameList.get(i);
			dataset = new Element("dataset");
			dataset.setAttribute("seriesName", nameStr);
			String rendType = cb.getNameRendAs(nameStr);
			if (rendType != null) {
				dataset.setAttribute("renderAs", rendType);
			}
			String color = cb.getCategoryColor(nameStr);
			if (color != null) {
				dataset.setAttribute("color", color);
			}
			if (cb.isShowValue()) {
				dataset.setAttribute("showvalues", "1");
			} else {
				dataset.setAttribute("showvalues", "0");
			}
			dataset.setAttribute("parentYAxis", cb.getCategoryParentYAxis(nameStr));
			for (int j = 0; j < categorieList.size(); j++) {
				categoryStr = (String) categorieList.get(j);
				cd = cb.getDataByCategoryAndName(categoryStr, nameStr);
				if (cb == null) {
					System.out.println("数据未设置，数据分类为" + categoryStr + "数据名称为"
							+ nameStr);
					cd.getName();
				}
				set = new Element("set");
				set.setAttribute("value", String.valueOf(cd.getValue()));
				if (cd.getShowValue() != null) {
					set.setAttribute("showValue", cd.getShowValue());
				}
				if(cd.getToolText()!=null)
				{
					set.setAttribute("toolText",cd.getToolText());
				}
				if (cd.getLinkType() == cb.linkType_custom) {
					set.setAttribute("link", cd.getUrl());
				} else if (cd.getLinkType() == cb.linkType_ajax) {
//					StringBuffer url = new StringBuffer();
//					url.append("javascript:_updateCharts('").append(chartDiv)
//							.append("','").append(chartId).append("','")
//							.append(cd.getSwf()).append("','").append(
//									cb.getWidth()).append("','").append(
//									cb.getHeight()).append("','").append(
//									cd.getClassname()).append("','").append(
//									cd.getParams()).append("','").append(
//									BaseConfig.getContextPath()).append("');");
//					set.setAttribute("link", url.toString());
				}
				dataset.addContent(set);

			}
			chart.addContent(dataset);
		}
		Element styles = getStylesElement(cb);
		if (styles != null) {
			chart.addContent(styles);
		}
		return doc;
	}
}
